package com.ph.controller;

import com.ph.pojo.Department;
import com.ph.pojo.Result;
import com.ph.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("sys/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;
//  树状全体查询
    @GetMapping
    public Result<List<Department>> findByTree() {
        return Result.success("查询成功",departmentService.findByTree());
    }
//    按照id查询
    @GetMapping("/{id}")
    public Result<List<Department>> findByParentId(@PathVariable Integer id) {
        return Result.success("查询成功",departmentService.findByParentId(id));
    }
// 添加
    @PostMapping
    public Result<Boolean> add(@RequestBody Department department){
        return Result.success("添加成功",departmentService.save(department));
    }
//    更新
    @PutMapping
    public Result<Boolean> update(@RequestBody Department department){
        return Result.success("更新成功",departmentService.updateById(department));
    }
//   删除
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Integer id){
        return Result.success("删除成功",departmentService.removeById(id));
    }
}
